﻿using msBsc.AppLogic.DataAlc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace msBsc.WebUi.wMall.Buyer
{
    public partial class wfAddOrder : msBsc.Cls.BPCustom
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //根据车辆id 获取车辆信息 
        public void GetCarInfo()
        {
            string userid = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(userid))
            {
                this.ExecJScript(@"layer.alert('登录过期,请重新登录.', 3);");
                return;
            }
            string carid = this.GetParamHtValue("carid");
            if (string.IsNullOrEmpty(carid))
            {
                this.ExecJScript("layer.alert('车辆信息错误', 3);"); return;
            }

            using (CBizVehicleInfo carinfo = new CBizVehicleInfo())
            {
                DataSet cards = carinfo.GetUserCarInfo(carid);
                if (cards == null || cards.Tables[0].Rows.Count < 1)
                {
                    this.ExecJScript("layer.alert('车辆信息错误', 3);"); return;
                }
                string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(cards.Tables[0], "", "", 1, 1000, "");

                this.ExecJScript(@"
                            var _html = $('#svc_list_temp_order').html();
                            laytpl(_html).render(" + retJson + @", function (render) {
                                $('#CarInfo').html(render);
                                $('#svc_list_temp_order').remove();
                            });");

                //获取改地接客 所有的车辆
                DataSet dsCarList = carinfo.GetVehicleForSubmitOrder(cards.Tables[0].Rows[0]["DJACK_ID"].ToString());
                if (dsCarList == null || dsCarList.Tables[0].Rows.Count < 1)
                {
                    this.ExecJScript("$('#carList').html(\"<p style='width: 200px; margin-left: 30px;' class='car-price'>没有查询到任何相关信息...</p>\");$('#divPage').html('');");
                    return;
                }

                retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dsCarList.Tables[0], "", "", 1, 1000, "");    
                this.ExecJScript(@"
                            var _html = $('#car_temp_list').html();
                            laytpl(_html).render(" + retJson + @", function (render) {
                                $('#carList').html(render);
                                $('#car_temp_list').remove();
                                jisuanSum(1);
                            });");


            }
        }
        //提交订单
        public void ComintCar()
        {
            string userid = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(userid))
            {
                this.ExecJScript(@"layer.alert('登录过期,请重新登录.', 3);");
                return;
            }
            string startime = this.GetParamHtValue("startime");
            if (string.IsNullOrEmpty(startime))
            {
                this.ExecJScript("layer.alert('开始时间不能为空.', 3);"); return;
            }
            string endtime = this.GetParamHtValue("endtime");
            if (string.IsNullOrEmpty(endtime))
            {
                this.ExecJScript("layer.alert('结束时间不能为空.', 3);"); return;
            }
            string carids = this.GetParamHtValue("carids");
            if (string.IsNullOrEmpty(carids))
            {
                this.ExecJScript("layer.alert('请先添加产品再确认订单.', 3);"); return;
            }

            using (CBizOrdersInfo orderinfo = new CBizOrdersInfo())
            {
                string[] msgs = orderinfo.AddOrderInfoN(carids, startime, endtime, "");
                if (Convert.ToBoolean(msgs[0]))
                {
                    this.ExecJScript("success('" + msgs[1] + "')");
                }
                else {
                    this.ExecJScript("layer.alert('" + msgs[1]+ ".', 3);"); return;
                }
            }
        }

        public override void DoAction()
        {
            if (this.strAction == "init")
            {
                GetCarInfo();
            }
            else if (this.strAction == "comintCar")
            {
                ComintCar();
            }
        }
    }
}